using System.Collections.ObjectModel;
using System.Windows;
using WpfApp1.Models;
using WpfApp1.Services;

namespace WpfApp1.ViewModels
{
    public class AdminDashboardViewModel
    {
        private readonly IStudentService _studentService;
        private readonly ICourseService _courseService;
        private readonly ICourseSelectService _courseSelectService;

        public ObservableCollection<Student> Students { get; set; } = new ObservableCollection<Student>();

        public AdminDashboardViewModel(IStudentService studentService, ICourseService courseService, ICourseSelectService courseSelectService)
        {
            _studentService = studentService;
            _courseService = courseService;
            _courseSelectService = courseSelectService;
        }

        //管理员-学生管理-搜索学生
        public void SearchStudent(string studentNumber)
        {
            if (string.IsNullOrEmpty(studentNumber))
            {
                Students.Clear();
                _studentService.GetStudents().ForEach(Students.Add);
            }
            else
            {
                var student = _studentService.GetStudentByNumber(studentNumber);
                Students.Clear();
                if (student != null)
                {
                    Students.Add(student);
                }
            }
        }

        //管理员-学生管理-添加学生
        public void AddStudent(Student student)
        {
            _studentService.AddStudent(student);
            SearchStudent(string.Empty);
        }

        //管理员-学生管理-编辑学生
        public void EditStudent(Student student)
        {
            _studentService.UpdateStudent(student);
            SearchStudent(string.Empty);
        }

        //管理员-学生管理-删除学生
        public void DeleteStudent(Student student)
        {
            _studentService.DeleteStudentByNumber(student);
            SearchStudent(string.Empty);
        }

        //课程管理
        public ObservableCollection<Course> Courses { get; } = new ObservableCollection<Course>();

        public void SearchCourse(string courseNumber)
        {
            Courses.Clear();
            if (string.IsNullOrEmpty(courseNumber))
            {
                var courses = _courseService.GetCourses();
                foreach (var course in courses)
                {
                    Courses.Add(course);
                }
            }
            else
            {
                var course = _courseService.GetCourseByNumber(courseNumber);
                if (course != null)
                {
                    Courses.Add(course);
                }
            }
        }

        //管理员-课程管理-添加课程
        public void AddCourse(Course course)
        {
            _courseService.AddCourse(course);
            SearchCourse(string.Empty);
        }

        //管理员-课程管理-编辑课程
        public void UpdateCourse(Course course)
        {
            _courseService.UpdateCourse(course);
            SearchCourse(string.Empty);
        }

        //管理员-课程管理-删除课程
        public void DeleteCourse(Course course)
        {
            _courseService.DeleteCourse(course);
            SearchCourse(string.Empty);
        }

        //选课管理
        public ObservableCollection<StudentCourseSelection> StudentCourses { get; } = new ObservableCollection<StudentCourseSelection>();

        //管理员-选课管理-加载所有选课
        public void SearchAllSelections()
        {
            StudentCourses.Clear();
            var selections = _courseSelectService.GetStudentCourses();
            foreach (var selection in selections)
            {
                StudentCourses.Add(new StudentCourseSelection(
                    selection.CourseNumber,
                    selection.CourseName,
                    selection.StudentNumber,
                    selection.StudentName,  // StudentName 在 StudentCourseDto 中是 Name
                    selection.Credits,
                    selection.Hours,
                    selection.TeacherName,  // TeacherName
                    selection.Grades
                ));
            }
        }

        //管理员-选课管理-搜索选课
        public void SearchSelectionByStudent(string studentNumber)
        {
            StudentCourses.Clear();
            var courseSelection = new CourseSelection(studentNumber, string.Empty, 0);
            var selections = _courseSelectService.AdminGetStudentSelectCoursesByNumber(courseSelection);
            foreach (var selection in selections)
            {
                StudentCourses.Add(selection);
            }
        }

        //管理员-选课管理-搜索选课
        public void SearchSelectionByCourse(string courseNumber)
        {
            StudentCourses.Clear();
            var courseSelection = new CourseSelection(string.Empty, courseNumber, 0);
            Console.WriteLine(courseSelection.CourseNumber);
            var selections = _courseSelectService.GetStudentCoursesByCourseNumber(courseSelection);
            foreach (var selection in selections)
            {
                StudentCourses.Add(selection);
            }
        }
    }
}